package com.framework.service.admin.impl;

import com.framework.bean.request.LoginRequest;
import com.framework.bean.response.CommonResponse;

import com.framework.service.admin.UserService;
import com.framework.utils.GsonTools;
import com.framework.utils.HttpClientUtil;
import com.framework.utils.SpringContextHolder;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class UserServiceImpl implements UserService {



    public CommonResponse login(String reqJson) throws Exception {
        LoginRequest req = GsonTools.str2T(reqJson,LoginRequest.class);
        CommonResponse resp = new CommonResponse();
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";
        url = String.format(url, "wx71fef7bf581db3e7","45bc6183c4a867cdbb6ae7f5b8c6fdf0",req.getCode());

        String response = HttpClientUtil.doPost(url, new HashMap<>(), "utf-8");

        Map<String, String> map = GsonTools.str2Map(response);
        resp.setMsg(map.get("openid"));
        return resp;
    }

    @Override
    public CommonResponse logout(String reqJson) throws Exception {
        CommonResponse resp = new CommonResponse();
        HttpSession session = SpringContextHolder.getSession();
        session.removeAttribute("user");
        resp.setFlag(true);
        resp.setMsg("退出成功!");
        return resp;
    }

    @Override
    public CommonResponse addUser(String reqJson) throws Exception {
        return null;
    }

    @Override
    public List<CommonResponse> users(String reqJson) throws Exception {
        return null;
    }

    @Override
    public CommonResponse getUserByUuid(String reqJson) throws Exception {
        return null;
    }

    @Override
    public CommonResponse deleteUser(String reqJson) throws Exception {
        return null;
    }
}
